<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-row-waiting-widget" :style="getStyle('head')">
      <div class="widget-inside" :style="getStyle('inside')">
        <div v-for="(item,key) in queuePatientInfos" class="queue" :style="getStyle('queue')" :key="`${widgetKey}-${item.workerId}-${key}`">
          <div class="queue-inside">
            <div class="dept" :style="getStyle('queueName')">
              <ScrollX>{{ item[optionsInfo.registerNameKey] || item['registerObjName'] || '\u00a0' }}</ScrollX>
            </div>
            <div class="wait-patient">
              <vue-seamless-scroll
                  class="scroll"
                  :hoverStop="false"
                  :data="getPatientList(item)"
                  @ScrollEnd="handleScrollEnd"
                  :class-option="classOptionPatient(getPatientList(item) || [])"
                  :ref="`scroll-${item.workerId}`"
              >
                <div class="inside-content">
                  <template v-if="!!(getPatientFillList(item)?.length)">
                    <template v-for="(patientItem,patientKey) in getPatientFillList(item)">
                      <div class="wait-patient-inside" :style="getStyle('waitPatient')" :key="patientKey">
                        <template v-for="(childItem,childKey) in  getPatientChildList(patientItem)">
                          <div
                              class="child-item"
                              :key="`${item.workerId}-${childKey}`"
                              v-html="getWaitPatientContent(childItem)"
                              :style="getWaitPatientStyle(childItem)"
                          />
                        </template>
                      </div>
                    </template>
                  </template>
                  <template v-else>
                    <div class="wait-patient-inside-empty"/>
                  </template>
                </div>
              </vue-seamless-scroll>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import emitter from "@/utils/designer/emitter";
import {pxToVw} from "@/utils/common";
import {v4 as uuid} from 'uuid';
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import vueSeamlessScroll from 'vue-seamless-scroll';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";

let buildParent = buildParentApi();
export default {
  name: "scene-waiting-row4-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  components: {
    ScrollX,
    CompOperate,
    vueSeamlessScroll
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),
    }
  },


  computed: {
    // 滚动配置项
    classOptionPatient() {
      return function (row) {
        let {openScroll, waitingNumber, waitingVisitStep, viewNumber, showColNums} = this.optionsInfo;
        let number = viewNumber * showColNums;
        return {
          step: [1, 1.5, 2, 2.5, 3][waitingVisitStep - 1], // 数值越大速度滚动越快
          hoverStop: true,
          direction: 1, // 0向下 1向上 2向左 3向右
          limitMoveNum: (openScroll && (row?.length >= number)) ? waitingNumber : 99999, // 开始无缝滚动的数据量
        };
      }
    },

    // 就诊和候诊的className名
    getClassName() {
      return function (val) {
        switch (val) {
          case "treatingPatient":
            return "current-patient";
          case "waitPatientList":
            return "wait-patient";
          case "waitPatientCount":
            return "patient-count";
          case "registerObjNames":
            return "register-name";
        }
      };
    },

    // 设置的各元素的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "height", pxToVw(info.contentHeight));
            return Obj;
          case "inside":
            // Reflect.set(Obj, "paddingTop", pxToVw(info?.padding?.top || 12));
            // Reflect.set(Obj, "paddingBottom", pxToVw(info?.padding?.bottom || 12));
            if(info?.padding?.top || info?.padding?.top == '0'){
              Reflect.set(Obj, "paddingTop", pxToVw(info?.padding?.top));
            }else{
               Reflect.set(Obj, "paddingTop", pxToVw(12));
            }
            if(info?.padding?.bottom || info?.padding?.bottom == '0'){
              Reflect.set(Obj, "paddingBottom", pxToVw(info?.padding?.bottom));
            }else{
               Reflect.set(Obj, "paddingBottom", pxToVw(12));
            }
            return Obj;
          case "queue":
            Reflect.set(Obj, "paddingLeft", pxToVw(info?.queuePadding?.value || 12));
            Reflect.set(Obj, "paddingRight", pxToVw(info?.queuePadding?.value || 12));
            return Obj;
          case "queueName":
            Reflect.set(Obj, "color", (info.queueNameFontColor));
            Reflect.set(Obj, "backgroundColor", (info.queueNameBackgroundColor));
            Reflect.set(Obj, "fontSize", pxToVw(info.queueNameFontSize));
            Reflect.set(Obj, "fontWeight", (info.queueNameFontWeight));
            return Obj;
          case "waitPatient":
            let listHeight = (info.contentHeight - info.padding.top - info.padding.bottom  - 90);
            if (info?.patientDivider) {
              Reflect.set(Obj, "borderColor", 'rgba(51, 51, 51, 0.2)');
            } else {
              Reflect.set(Obj, "borderColor", 'transparent');
            }
            Reflect.set(Obj, "lineHeight", pxToVw(listHeight / info.viewNumber));
            Reflect.set(Obj, "height", pxToVw(listHeight / (info.viewNumber)));
            Reflect.set(Obj, "fontSize", pxToVw(info.patientFontSize));
            Reflect.set(Obj, "fontWeight", (info.patientFontWeight));
            return Obj;
        }
      };
    },

    // 列表的队列的患者queue
    queuePatientInfos() {
      let devType = (this.devType);
      let {showRowNums} = (this.optionsInfo);
      let queuePatientList = this.$store.getters['getWaitingPatientInfo'];
      let listInfo = devType ? (queuePatientList.apiData) : (queuePatientList.mockData);
      if (listInfo?.length > showRowNums) {
        return listInfo.slice(0, showRowNums);
      } else {
        let arr = (new Array(showRowNums - listInfo?.length).fill({}));
        return [...listInfo, ...arr];
      }
    },

    // 合并当前队列的候诊就诊患者
    getPatientList() {
      return function (row) {
        let {showCurrentPatient = 2} = this.optionsInfo;
        let currentPatient = row?.treatingPatient || []
        let waitPatient = row?.waitPatientList || [];
        if (showCurrentPatient === 2) {
          return [...currentPatient, ...waitPatient]
        } else {
          return [...waitPatient]
        }
      }
    },

    // 合并当前队列的候诊就诊患者并填充数据
    getPatientFillList() {
      return function (row) {
        let listInfo;
        let {viewNumber = 8, showColNums = 6} = this.optionsInfo;
        let mergePatient = this.getPatientList(row);
        let number = viewNumber * showColNums;
        if (mergePatient?.length) {
          let len = mergePatient?.length;
          if (len >= number) {
            listInfo = mergePatient
          } else {
            let fillArr = Array(number - len).fill({});
            listInfo = [...mergePatient, ...fillArr]
          }
        } else {
          listInfo = Array(number).fill({});
        }
        return this.$lodash.chunk(listInfo, showColNums)
      }
    },

    // 二级分类列表
    getPatientChildList() {
      return function (row) {
        let {showColNums} = this.optionsInfo;
        let len = row?.length;
        if (len >= showColNums) {
          return row
        } else {
          let fillArr = Array(showColNums - len).fill({});
          return [...row, ...fillArr]
        }
      }
    },

    //候诊患者信息
    getWaitPatientContent() {
      return function (row) {
        let orderNoInfo;
        let {orderTag, showRoomName, patientRoomNameKey, roomNamePosition = 'right',containHangupData} = this.optionsInfo;
        if(containHangupData && row?.bgColor){
          //  orderNoInfo = row.orderPrefix + (row?.orderNumber || '\u00a0')
          orderNoInfo = (row?.colorPrefix || '\u00a0') + (row.orderPrefix || '\u00a0') + (row?.orderNumber || '\u00a0')
        } else if (orderTag && (row?.orderTagColor)) {
          orderNoInfo = (row.orderNo) ?
              `<span style='color:${row?.orderTagColor || 'transparent'}'>${row?.orderPrefix || '\u00a0'}</span>${row?.orderNumber || '\u00a0'}` : ''
        } else {
          orderNoInfo = `${row?.orderNo || '\u00a0'}`
        }
        // 1: 无诊室 2:诊室
        switch (showRoomName) {
          case 1:
            if (!!this.$lodash.trim(orderNoInfo)) {
              return `${orderNoInfo} ${row?.patientName || '\u00a0'}`
            } else {
              return `${row?.patientName || '\u00a0'}`
            }
          case 2:
            let patientInfo;
            if (!!this.$lodash.trim(orderNoInfo)) {
              patientInfo = `${orderNoInfo} ${row?.patientName || '\u00a0'}`
            } else {
              patientInfo = `${row?.patientName || '\u00a0'}`
            }
            if (roomNamePosition === 'right') {
              return `${patientInfo || '\u00a0'} ${row[patientRoomNameKey] || row['treatmentRoomName'] || '\u00a0'}`
            } else {
              return `${row[patientRoomNameKey] || row['treatmentRoomName'] || '\u00a0'} ${patientInfo || '\u00a0'}`
            }
        }
      }
    },
    // 候诊患者 样式
    getWaitPatientStyle() {
      return function (row) { 
        let {containHangupData = false} = this.optionsInfo;
        if(row.bgColor && containHangupData){
          let Obj = Object();
          Reflect.set(Obj, "backgroundColor", (row.bgColor));
          Reflect.set(Obj, "color", (row.ftColor));
          return Obj
        }
      }
    }
  },

  methods: {
    // 等待候诊滚动结束
    handleScrollEnd() {
      console.log('等待就诊滚动结束');
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          let patientList = this.$lodash.cloneDeep(data);
          this.$store.dispatch('setWaitingPatientInfo', patientList)
          break
      }
    }
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-row-waiting-widget {
  color: #333333;
  display: flex;
  flex-direction: row;
  align-self: center;
  align-items: center;
  width: 100%;
  overflow: hidden;
  background-color: transparent;
  box-sizing: border-box;

  .widget-inside {
    display: flex;
    flex: 1;
    width: 100%;
    align-self: start;
    height: 100%;
    padding: 28px 0;
    box-sizing: border-box;
    flex-direction: row;
  }

  .queue {
    flex-basis: 0;
    flex-grow: 1;
    height: 100%;
    font-size: 46px;
    overflow: hidden;
    padding: 0 12px;
    text-align: center;
    box-sizing: border-box;

    &:first-child {
      padding-left: 24px !important;
    }

    &:last-child {
      padding-right: 24px !important;
    }

    .queue-inside {
      border-radius: 18px;
      overflow: hidden;
      width: 100%;
      height: 100%;

      .dept {
        color: white;
        padding: 0 18px;
        font-size: 46px;
        height: 90px;
        background: rgba(0, 89, 179, 1);
        display: flex;
        flex-basis: 100%;
        text-align: center;
        align-self: center;
        align-items: center;
        justify-content: center;
      }

      .current-patient {
        background: rgba(51, 122, 194, 1);
        padding: 24px 0 22px 0;
        color: white;
        font-size: 45px;
        min-height: 86px;
        @include singleEllipsis(100%);
        display: block;
      }

      .wait-patient {
        display: flex;
        width: 100%;
        height: calc(100% - 90px);
        background: white;
        overflow: hidden;

        .scroll {
          width: 100%;
        }

        .inside-content {
          display: flex;
          flex-direction: column;
          width: 100%;
          align-items: center;
          align-self: center;
          align-content: center;
          justify-content: center; /* 如果你也想水平居中 */

          .wait-patient-inside {
            color: #333333;
            box-sizing: border-box;
            font-size: 32px;
            width: 100%;
            display: flex;
            flex-direction: row;
            overflow: hidden;
            background: rgba(255, 255, 255, 1);
            text-align: center;
            border-bottom: 1px rgba(51, 51, 51, 0.2) solid;

            .child-item {
              flex-grow: 1;
              flex-shrink: 0;
              flex-basis: 0;
              @include singleEllipsis(100%)
            }
          }
        }


        .wait-patient-inside-empty {
          min-height: 480px;
          margin-top: 10px;
          background: white;
        }
      }
    }
  }
}
</style>